﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.IO;


namespace AAA.Web
{
    public class WebHelper
    {
        public static string GetIPAddress(HttpRequest request)
        {
            if (request.ServerVariables["HTTP_VIA"] == null)
            {
                return request.ServerVariables["REMOTE_ADDR"].ToString();
            }
            else
            {
                return request.ServerVariables["HTTP_X_FORWARDED_FOR"].ToString();
            }
            return null;
        }

        public void DownloadServerFile(string strUserFileName, string strServerFileName)
        {
            FileStream inputFileStream = null;
            long lFileSize;
            byte[] bBuffers;

            try
            {


                inputFileStream = new FileStream(strServerFileName, FileMode.Open);
                lFileSize = inputFileStream.Length;

                bBuffers = new byte[(int)lFileSize];
                inputFileStream.Read(bBuffers, 0, (int)lFileSize);

                DownloadServerFile(strUserFileName, bBuffers);
            }
            catch (Exception ex)
            {
                throw;
            }
            finally
            {
                inputFileStream.Close();
            }
        }

        public void DownloadServerFile(string strUserFileName, byte[] bFileContents)
        {

            HttpResponse response;
            try
            {
                
                response = HttpContext.Current.Response;

                response.ClearHeaders();
　　            response.Clear();
　　            response.Expires = 0;
　　            response.Buffer = true;
                response.AddHeader("Accept-Language", "zh-tw");                
                response.AddHeader("content-disposition", "attachment; filename=\"" + System.Web.HttpUtility.UrlEncode(strUserFileName, System.Text.Encoding.UTF8) + "\"");
　　            response.ContentType = "Application/octet-stream";
　　            //檔案內容
               
　　            response.BinaryWrite(bFileContents);
　　            response.End();


            }
            catch (Exception ex)
            {
                throw;
            }
        }

        //    string strRemoteAddr = request.UserHostAddress;
        //    string strXForwardedFor = request.ServerVariables["X_FORWARDED_FOR"];
        //    string strIP = "";

        //    if (strXForwardedFor == null)
        //    {
        //        strIP = strRemoteAddr;
        //    }
        //    else
        //    {
        //        strIP = strXForwardedFor;
        //        if (strIP.IndexOf(",") > 0)
        //        {
        //            string[] arIPs = strIP.Split(',');

        //            foreach (string item in arIPs)
        //            {
        //                if (!isPrivateIP(item))
        //                {
        //                    return item;
        //                }
        //            }
        //        }
        //    }
        //    return strIP;
        //}
    }
}
